package com.x.erp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.MultilevelActivityCash;
import com.x.erp.rpc.MultilevelActivityAccountRpcService;
import com.x.erp.rpc.MultilevelActivityCashRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 多级营销提现记录信息
 */
@RestController
@RequestMapping("/multilevelActivityCash")
public class MultilevelActivityCashController extends BaseController{

	@Reference
	private MultilevelActivityCashRpcService multilevelActivityCashRpcService;// 多级营销提现记录信息服务

	@Reference
	private MultilevelActivityAccountRpcService multilevelActivityAccountRpcService;// 多级营销账户信息服务

	/**
	 * 查询多级营销提现记录信息列表
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getMultilevelActivityCashList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getMultilevelActivityCashList(MultilevelActivityCash multilevelActivityCash) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", multilevelActivityCashRpcService.selectMultilevelActivityCashList(multilevelActivityCash));
	}

	/**
	 * 查询多级营销提现记录信息数量
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getMultilevelActivityCashNum", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getMultilevelActivityCashNum(MultilevelActivityCash multilevelActivityCash) {
		Map<String, Object> res = new HashMap<>();
		res.put("total", multilevelActivityCashRpcService.selectMultilevelActivityCashNum(multilevelActivityCash));
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 根据用户ID查询多级营销提现记录信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getMultilevelActivityCash", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getMultilevelActivityCash(MultilevelActivityCash multilevelActivityCash) {
		if(multilevelActivityCash == null || StringUtils.isEmpty(multilevelActivityCash.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		//查询app当前版本
		List<MultilevelActivityCash> multilevelActivityCashs = multilevelActivityCashRpcService.selectMultilevelActivityCashList(multilevelActivityCash);
		if(multilevelActivityCashs != null && multilevelActivityCashs.size() > 0) {
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", multilevelActivityCashs.get(0));
		}
		return new ResultModel(ResultCode.RESULT_FAIL, "查询失败");
	}

	/**
	 * 新增多级营销提现记录信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/insertMultilevelActivityCash", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertMultilevelActivityCash(MultilevelActivityCash multilevelActivityCash) {
		if(multilevelActivityCash == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "多级营销提现记录信息不能为空！");
		}
		if(StringUtils.isEmpty(multilevelActivityCash.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		MultilevelActivityCash cash = new MultilevelActivityCash();
		cash.setUserId(multilevelActivityCash.getUserId());
		Date cDate = this.getDate(0);
		multilevelActivityCash.setCashCreateTime(cDate);
		multilevelActivityCash.setCashUpdateTime(cDate);
		multilevelActivityCash.setCashState(1);
		MultilevelActivityCash cashBack = multilevelActivityCashRpcService.insertMultilevelActivityCash(multilevelActivityCash);
		if (cashBack == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "提现失败！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功！", cashBack);
	}

	/**
	 * 修改多级营销提现记录信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/updateMultilevelActivityCash", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel updateMultilevelActivityCash(MultilevelActivityCash multilevelActivityCash) {
		if(multilevelActivityCash == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "多级营销提现记录信息不能为空！");
		}
		if(StringUtils.isEmpty(multilevelActivityCash.getCashId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "提现ID不能为空！");
		}
		MultilevelActivityCash cash = new MultilevelActivityCash();
		cash.setCashId(multilevelActivityCash.getCashId());
		Date cDate = this.getDate(0);
		multilevelActivityCash.setCashUpdateTime(cDate);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功！", multilevelActivityCashRpcService.updateMultilevelActivityCash(multilevelActivityCash));
	}

}
